﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;
using blackhouse.Locations;

namespace blackhouse.BlackApart.Business.Apartments {
    public class DaApartmentLocation : IApartmentLocation {

        #region Fields

        private ImpApartmentLocation instance;
        private IApartmentLocationDataAccess iApartmentLocationData;

        #endregion

        #region Properties

        public int Id {
            get { return this.instance.Id; }
            private set { this.instance.Id = value; }
        }

        public Location Localization {
            get { return this.instance.Localization; }
            set { this.instance.Localization = value; }
        }
        public string LocationName {
            get { return this.instance.LocationName; }
            set { this.instance.LocationName = value; }
        }
		public int CityId {
			get { return this.instance.CityId; }
			set { this.instance.CityId = value; }
		}

		public string City {
			get { return this.instance.City; }
			set { this.instance.City = value; }
		}

		public int DistrictId {
			get { return this.instance.DistrictId; }
			set { this.instance.DistrictId = value; }
		}

		public string District {
			get { return this.instance.District; }
			set { this.instance.District = value; }
		}

        #endregion

        #region Constructors

        public DaApartmentLocation(IApartmentLocationDataAccess iApartmentLocationDataAccess) {
            this.iApartmentLocationData = iApartmentLocationDataAccess;
            this.instance = new ImpApartmentLocation();
        }

        public DaApartmentLocation(IApartmentLocationDataAccess iApartmentLocationDataAccess, int locationId) {
            this.iApartmentLocationData = iApartmentLocationDataAccess;
            this.instance = this.ConvertToInstance(this.iApartmentLocationData.Get(locationId));
        }

        #endregion

        #region Methods

        public bool Save() {
            if (this.instance.Save()) {
                this.Id = this.iApartmentLocationData.Save(this.Id, this.Localization.Latitude, this.Localization.Longitude, this.LocationName);
                return this.Id > 0;
            } else return false;
        }

        private ImpApartmentLocation ConvertToInstance(ApartmentLocationData data) {
            ImpApartmentLocation location = new ImpApartmentLocation();
            location.Id = data.Id;
            location.Localization = new Location() { Latitude = data.Latitude, Longitude = data.Longitude };
            location.LocationName = data.Name;
			location.CityId = data.CityId;
			location.City = data.City;
			location.DistrictId = data.DistrictId;
			location.District = data.District;
            return location;
        }

        #endregion

        #region Equal Methods

        public override bool Equals(object obj) {
            DaApartmentLocation location = obj as DaApartmentLocation;
            if (location == null) return false;
            return location.Id.Equals(this.Id);
        }

        public override int GetHashCode() {
            return this.Id;
        }

        public override string ToString() {
            return string.Format("{0} ({1})", this.LocationName, this.Id);
        }

        #endregion

    }
}
